package com.jzo2o.health.handler;


import com.alibaba.fastjson.JSON;
import com.jzo2o.api.trade.enums.TradingStateEnum;
import com.jzo2o.common.constants.MqConstants;
import com.jzo2o.common.model.msg.TradeStatusMsg;
import com.jzo2o.common.utils.ObjectUtils;
import com.jzo2o.health.service.IOrdersCreateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.List;

@Component
@Slf4j
public class TradeStatusMsgHandler {
    @Resource
    private IOrdersCreateService ordersCreateService;

    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(MqConstants.Queues.ORDERS_TRADE_UPDATE_STATUS),
            exchange = @Exchange(value = MqConstants.Exchanges.TRADE,type = ExchangeTypes.TOPIC),
            key = MqConstants.RoutingKeys.TRADE_UPDATE_STATUS
    ))
    public void handleTradeStatusMsg(String msg) {
        log.info("监听到支付状态通知消息:{}",msg);
        List<TradeStatusMsg> list = JSON.parseArray(msg, TradeStatusMsg.class);
        for (TradeStatusMsg tradeStatusMsg : list) {
            if (ObjectUtils.equals("jzo2o.health",tradeStatusMsg.getProductAppId())
            && ObjectUtils.equals(tradeStatusMsg.getStatusCode(), TradingStateEnum.YJS.getCode())) {
                ordersCreateService.paySuccess(tradeStatusMsg);
            }
        }

    }
}
